<?php
/*
 * Copyright (c) 2013 Eugene Kuzmenko
 *
 * Permission  is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in  the Software without restriction, including without limitation the rights
 * to  use,  copy,  modify,  merge, publish, distribute, sublicense, and/or sell
 * copies  of  the  Software,  and  to  permit  persons  to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The  above  copyright  notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE  IS  PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED,  INCLUDING  BUT  NOT  LIMITED  TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS  FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS  OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR  ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * For more information, see <http://www.aljey.com>.
 */

namespace Al\Core\Lib;

use Al\HTTP\Lib\Response;

/**
 * AlWEB base controller provides common functionality to the controllers that
 * extend it, however extending it is not mandatory.
 * 
 * @author Eugene Kuzmenko <mail@aljey.com>
 */
class Controller {

  /**
   * @var string The name of a plugin that contains a controller.
   */
  private $plugin_name;

  /**
   * @var string The web root of a plugin that contains a controller.
   */
  private $web_root;

  /**
   * Retrieves a plugin configuration value, which can be created under the
   * "plugins" configuration key:
   * <code>
   * plugins:
   *   Acme\Blog:
   *     entry:
   *       limit_per_page: 20
   * </code>
   * Such configuration can now be retrieved simply by:
   * <code>
   * $this->getConfig('entry.limit_per_page');
   * </code>
   *
   * @param string $name A dot separated configuration key name.
   * @return mixed A configured value.
   */
  protected function getConfig($name) {
    return Service::get('config.plugins.' . $this->getPluginName() . ".$name");
  }

  /**
   * Returns the name of a plugin (eg. "Acme\Blog") that contains your
   * controller.
   *
   * @return string The name of a plugin that contains a controller.
   */
  protected function getPluginName() {
    if (!$this->plugin_name) {
      $function = new \ReflectionClass(get_class($this));
      $namespace = explode('\\', $function->getNamespaceName());
      array_pop($namespace);
      $this->plugin_name = implode('\\', $namespace);
    }
    return $this->plugin_name;
  }

  /**
   * Returns the web root of a plugin (eg. "/plugin/AcmeBlog") that contains
   * your controller.
   *
   * @return string The web root of a plugin that contains a controller.
   */
  protected function getWebRoot() {
    if (!$this->web_root) {
      $this->web_root = Service::get('config')->getPluginWebRoot($this
        ->getPluginName());
    }
    return $this->web_root;
  }

  /**
   * Renders a template without outputting the results to the browser.
   *
   * @param string $template A colon separated template name (see $this#render).
   * @param array $params [optional] An array of variables to pass to the
   *                      templating engine.
   * @return string The result of rendering the template file.
   */
  protected function getText($template, $params = array()) {
    if (!isset($params['plugin'])) $params['plugin'] = $this->getPluginName();
    $result = Service::get('template')->render($template, $params);
    return $result[0];
  }

  /**
   * Render a template to the browser.
   *
   * @param string $template @see Template::findTemplate()
   * @param array $params [optional] An array of variables to pass to the
   *                      templating engine.
   * @param string $extension @see Controller::getRenderer()
   * @return Response
   */
  protected function render($template, $params = array(), $extension = null) {
    if (!isset($params['plugin'])) $params['plugin'] = $this->getPluginName();
    $result = Service::get('template')->render($template, $params, $extension);
    return new Response($result[0], $result[1]);
  }

  /**
   * Render text directly.
   *
   * @param string $data [optional] Renders a string.
   * @param string $extension [optional] An extension denoting the type of the
   *                          content in the file, defaults to "html".
   * @return Response
   */
  protected function renderText($data = '', $extension = 'html') {
    return new Response($data, $extension);
  }

  /**
   * Convenience method to render json encoded text.
   *
   * @param array $data [optional] The data to encode.
   * @param string $extension [optional] An extension denoting the type of the
   *                          content in the file, defaults to "html".
   * @return Response
   */
  protected function renderJson($data = array(), $extension = 'html') {
    return $this->renderText(json_encode($data), $extension);
  }

}